{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "from langchain_openai import ChatOpenAI\n",
    "\n",
    "_llm = ChatOpenAI(\n",
    "    api_key=\"ollama\",\n",
    "    model=\"qwen2.5:7b\",\n",
    "    base_url=\"http://192.168.10.11:60026/v1\",\n",
    "    temperature=0.7,\n",
    ")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "AIMessage(content='您好！不过我无法提供实时信息，包括具体的上海市今天的天气情况。建议您查询最新的 weather.com 或者使用相关的天气应用查看上海今天的天气。一般里面会包含温度、湿度、风速以及降雨概率等信息。您可以根据需要做好相应的准备。如果您有其他问题或需要其他帮助，欢迎随时告诉我！', additional_kwargs={'refusal': None}, response_metadata={'token_usage': {'completion_tokens': 69, 'prompt_tokens': 35, 'total_tokens': 104, 'completion_tokens_details': None}, 'model_name': 'qwen2.5:7b', 'system_fingerprint': 'fp_ollama', 'finish_reason': 'stop', 'logprobs': None}, id='run-8ba16283-831c-4529-87ab-1942af1d2ed7-0', usage_metadata={'input_tokens': 35, 'output_tokens': 69, 'total_tokens': 104, 'input_token_details': {}, 'output_token_details': {}})"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "_llm.invoke(\"今天上海的天气如何?\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import Annotated\n",
    "from langchain_core.tools import tool\n",
    "\n",
    "\n",
    "@tool\n",
    "def weather(city: Annotated[str, \"被查询的城市,用中文输入\"]) -> str:\n",
    "    \"\"\"\n",
    "    用于查询输入城市今日的天气状况。\n",
    "    \"\"\"\n",
    "    if city == \"上海\":\n",
    "        return \"上海今日有台风12级\"\n",
    "    else:\n",
    "        return \"天气晴朗,风和日丽\"\n",
    "\n",
    "\n",
    "@tool\n",
    "def fish(river: Annotated[str, \"被查询的河流,用中文输入\"]) -> str:\n",
    "    \"\"\"\n",
    "    用于查询输入河流有哪些鱼类\n",
    "    \"\"\"\n",
    "    if \"新安江\" in river:\n",
    "        return \"江里有石斑鱼、鲫鱼、草鱼、黑鱼、花鲢、青鱼\"\n",
    "    else:\n",
    "        return \"风浪越大鱼越贵\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [],
   "source": [
    "_tools = [\n",
    "    weather,\n",
    "    fish,\n",
    "]\n",
    "_llm_with_tools = _llm.bind_tools(_tools)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "metadata": {},
   "outputs": [],
   "source": [
    "_messages = [\n",
    "    (\"human\", \"建德江里有什么?\"),\n",
    "]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "AIMessage(content='', additional_kwargs={'tool_calls': [{'id': 'call_nrgrljeo', 'function': {'arguments': '{\"river\":\"建德江\"}', 'name': 'fish'}, 'type': 'function'}], 'refusal': None}, response_metadata={'token_usage': {'completion_tokens': 21, 'prompt_tokens': 222, 'total_tokens': 243, 'completion_tokens_details': None}, 'model_name': 'qwen2.5:7b', 'system_fingerprint': 'fp_ollama', 'finish_reason': 'tool_calls', 'logprobs': None}, id='run-d5a1d06b-cc42-47b0-b759-f1e31461d592-0', tool_calls=[{'name': 'fish', 'args': {'river': '建德江'}, 'id': 'call_nrgrljeo', 'type': 'tool_call'}], usage_metadata={'input_tokens': 222, 'output_tokens': 21, 'total_tokens': 243, 'input_token_details': {}, 'output_token_details': {}})"
      ]
     },
     "execution_count": 25,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "rep = _llm_with_tools.invoke(_messages)\n",
    "rep"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'风浪越大鱼越贵'"
      ]
     },
     "execution_count": 27,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "from langchain_core.messages import ToolMessage\n",
    "\n",
    "if rep.tool_calls is not None or len(rep.tool_calls) > 0:\n",
    "    _messages.append(rep)\n",
    "    for _tool in rep.tool_calls:\n",
    "        _fun = eval(_tool[\"name\"])\n",
    "        _tool_rep = _fun.invoke(_tool[\"args\"])\n",
    "        # print(_tool_rep)\n",
    "        _messages.append(_tool_rep)\n",
    "# _tool_rep\n",
    "_llm.invoke(_messages)\n",
    "print(_messages)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "python310",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.10.14"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
